import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import { companyInfo } from "./api/api";
import { productDetail, articlDetail } from "./api/api";
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'
import iView from 'iview';
import 'iview/dist/styles/iview.css';    // 使用 CSS

Vue.prototype.$bus = new Vue()

Vue.config.productionTip = false
Vue.use(iView);
Vue.use(ElementUI)
Vue.use(VueAwesomeSwiper)


router.beforeEach((to, from, next) => {
  // Vue.$root.$emit('change')
  // if (store.getters['userId']) {
  //   store.commit("settoekn", "");
  //   store.commit("setUserId", "");
  //   // router.push("/");
  // }

  if (to.path == '/index') {
    companyInfo().then(res => {
      window.document.title = res.data.title == undefined ? '兰舟智能' : res.data.title
      window.document.querySelector('meta[name="keywords"]').setAttribute('content', res.data.keywords)
      window.document.querySelector('meta[name="description"]').setAttribute('content', res.data.description)
      //  meta.content = to.meta.content;
      //  head[0].appendChild(meta)
    })
  } if (to.path == '/productDetail') {
    productDetail({ id: to.query.userId }).then(res => {
      window.document.title = res.data.storeName == undefined ? '兰舟智能' : res.data.storeName + '-兰舟智能'
      window.document.querySelector('meta[name="keywords"]').setAttribute('content', res.data.storeName)
      window.document.querySelector('meta[name="description"]').setAttribute('content', res.data.storeName)
    })
  } if (to.path == '/newsDetail') {
    articlDetail({ id: to.query.id }).then(res => {
      window.document.title = res.data.title == undefined ? '兰舟智能' : res.data.title + '-兰舟智能'
      window.document.querySelector('meta[name="keywords"]').setAttribute('content', res.data.title)
      window.document.querySelector('meta[name="description"]').setAttribute('content', res.data.synopsis)
    })
  } else {
    window.document.title = to.meta.title == undefined ? '兰舟智能' : to.meta.title
    document.querySelector('meta[name="keywords"]').setAttribute('content', to.meta.keywords)
    document.querySelector('meta[name="description"]').setAttribute('content', to.meta.description)
  }
  store.commit('settoRouter', to.path)
  let p = store.getters['move']
  store.commit('setMove', !p)
  next()
})

new Vue({
  router,
  store,
  render: function (h) { return h(App) }
}).$mount('#app')
